package com.forlove.app.qselect.login.presenter;

import android.text.TextUtils;

import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.login.entity.LoginEntity;
import com.forlove.app.qselect.login.entity.UserEntity;
import com.forlove.app.qselect.login.service.IResetPwService;
import com.forlove.app.qselect.login.service.impl.ResetPwServiceImpl;
import com.forlove.app.qselect.login.view.ResetPwView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yjl on 2017/10/30.
 */

public class ResetPwPresenter extends IBasePresenter {
    private IResetPwService resetPwService;
    private ResetPwView resetPwView;

    public ResetPwPresenter(ResetPwView resetPwView) {
        this.resetPwView = resetPwView;
        if (resetPwService == null) {
            resetPwService = new ResetPwServiceImpl();
            setService(resetPwService);
        }
    }


    public void loadSetPWData(final LoginEntity loginEntity) {
        Map<String, String> param = new HashMap<>();
        param.put("mobile", loginEntity.mobile);
        param.put("valid_token", loginEntity.valid_token);
        param.put("mobile_type", loginEntity.mobile_type);
        param.put("device_no", loginEntity.device_no);
        param.put("location", loginEntity.location);
        param.put("password", loginEntity.password);
        resetPwService.loadData(ServerManager.ACTION_RESET_PW, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "重设密码:" + result);
                UserEntity userEntity = new UserEntity();
                userEntity.parseJSONString(ParseHelper.getString(result, "data"));
                userEntity.password = loginEntity.password;
                userEntity.lastloc = loginEntity.location;
                userEntity.device_no = loginEntity.device_no;
                resetPwView.showSetPassWordView(userEntity);

            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    resetPwView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    resetPwView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }

        });
    }
}
